<?php
class Xml extends MY_Controller {
    public function __construct() {
        parent::__construct();
    }
    
    public function index() {
        //Pre-load View
        $this->_data['load_page'] = 'xml/index_view';
        //Process

        //Load View        
        $this->load->view($this->_data['template'], $this->_data);
    }
    
    public function display() {
        $this->load->model('Mxml');
        $data = $this->Mxml->listAll();
        extract($data);
        $xml = new SimpleXMLElement("<lvb_system></lvb_system>");
        //$xml->addAttribute('encoding','UTF-8');
        //Line Element
        foreach ($line as $item) {
            //Vehicle Counter
            $count = 0;
            foreach ($vehicle as $count_vehicles) {
                if ($item['id'] == $count_vehicles['line_id']) {
                    $count++;
                }
            }
            /////
            $xml_line = $xml->addChild('line');
            $xml_line->addAttribute('id', $item['id']);
            $xml_line->addChild('code', $item['code']);
            $xml_line->addChild('type', $item['type']);
            $xml_line->addChild('start_time_operation', $item['start_time_operation']);
            $xml_line->addChild('end_time_operation', $item['end_time_operation']);
            $xml_line->addChild('count_vehicles', $count);
            $xml_line->addChild('map', $item['map']);
            $start_station = $xml_line->addChild('start_station');
            $end_station = $xml_line->addChild('end_station');
            foreach ($station as $item2) {
                //Add Start-End ID
                if ($item2['position_station'] == 'START') {
                    $start_station->addAttribute('id', $item2['id']);     
                    $start_station->addChild('name', $item2['name']);                     
                } elseif ($item2['position_station'] == 'END') {
                    $end_station->addAttribute('id', $item2['id']);
                    $end_station->addChild('name', $item2['name']); 
                }
            }
            foreach ($station as $item2) {
                $count++;
                if ($item2['line_id'] == $item['id'] && $item2['position_station'] == '' || $item2['position_station'] == 'INTER' ) {
                    $xml_station = $xml_line->addChild('intermediate_stations');
                    $xml_station->addChild('name', $item2['name']);
                    $xml_station->addAttribute('id', $item2['id']);            
                }        
            }
            $count =0;
            foreach ($vehicle as $item3) {
                if ($item3['line_id'] == $item['id']) {
                    $xml_vehicle = $xml_line->addChild('vehicles_line');
                    $xml_vehicle->addChild('name', $item3['name']);
                    $xml_vehicle->addChild('capacity', $item3['capacity']);
                    $xml_vehicle->addAttribute('id', $item3['id']);            
                    foreach ($driver as $item4) {
                        if ($item4['vehicle_id'] == $item3['id']) {
                            $xml_driver = $xml_vehicle->addChild('driver');    
                            $xml_driver->addChild('name', $item4['name']);
                            $xml_driver->addChild('birth_date', $item4['birth_date']);
                            $xml_driver->addChild('email', $item4['email']);
                            $xml_driver->addChild('phone', $item4['phone']);
                            $xml_driver->addChild('avatar', $item4['avatar']);
                            $xml_driver->addAttribute('id', $item4['id']); 
                        }
                    }
                }
            }
        }
        header('Content-Type: text/xml');
        echo $xml->asXML();
    }
    
    public function download() {
        $this->load->model('Mxml');
        $data = $this->Mxml->listAll();
        extract($data);
        $xml = new SimpleXMLElement("<lvb_system></lvb_system>");
        //$xml->addAttribute('encoding','UTF-8');
        //Line Element
        foreach ($line as $item) {
            //Vehicle Counter
            $count = 0;
            foreach ($vehicle as $count_vehicles) {
                if ($item['id'] == $count_vehicles['line_id']) {
                    $count++;
                }
            }
            /////
            $xml_line = $xml->addChild('line');
            $xml_line->addAttribute('id', $item['id']);
            $xml_line->addChild('code', $item['code']);
            $xml_line->addChild('type', $item['type']);
            $xml_line->addChild('start_time_operation', $item['start_time_operation']);
            $xml_line->addChild('end_time_operation', $item['end_time_operation']);
            $xml_line->addChild('count_vehicles', $count);
            $xml_line->addChild('map', $item['map']);
            $start_station = $xml_line->addChild('start_station');
            $end_station = $xml_line->addChild('end_station');
            foreach ($station as $item2) {
                //Add Start-End ID
                if ($item2['position_station'] == 'START') {
                    $start_station->addAttribute('id', $item2['id']);     
                    $start_station->addChild('name', $item2['name']);                     
                } elseif ($item2['position_station'] == 'END') {
                    $end_station->addAttribute('id', $item2['id']);
                    $end_station->addChild('name', $item2['name']); 
                }
            }
            foreach ($station as $item2) {
                $count++;
                if ($item2['line_id'] == $item['id'] && $item2['position_station'] == '' || $item2['position_station'] == 'INTER' ) {
                    $xml_station = $xml_line->addChild('intermediate_stations');
                    $xml_station->addChild('name', $item2['name']);
                    $xml_station->addAttribute('id', $item2['id']);            
                }        
            }
            $count =0;
            foreach ($vehicle as $item3) {
                if ($item3['line_id'] == $item['id']) {
                    $xml_vehicle = $xml_line->addChild('vehicles_line');
                    $xml_vehicle->addChild('name', $item3['name']);
                    $xml_vehicle->addChild('capacity', $item3['capacity']);
                    $xml_vehicle->addAttribute('id', $item3['id']);            
                    foreach ($driver as $item4) {
                        if ($item4['vehicle_id'] == $item3['id']) {
                            $xml_driver = $xml_vehicle->addChild('driver');    
                            $xml_driver->addChild('name', $item4['name']);
                            $xml_driver->addChild('birth_date', $item4['birth_date']);
                            $xml_driver->addChild('email', $item4['email']);
                            $xml_driver->addChild('phone', $item4['phone']);
                            $xml_driver->addChild('avatar', $item4['avatar']);
                            $xml_driver->addAttribute('id', $item4['id']); 
                        }
                    }
                }
            }
        }
        header('Content-Type: text/xml');
        $str = $xml->asXML();
        $this->load->helper('download');
        force_download('myfile.xml', $str);
    }
    
    public function test() {
        $this->load->model('Mxml');
        $data = $this->Mxml->listAll();
        extract($data);
        //echo '<pre>';
        //print_r($data);
        //echo '</pre>';
        $xml = new XMLWriter();
        $xml->openMemory();
        $xml->setIndent(true);
        $xml->setIndentString(' ');
        $xml->startDocument('1.0','UTF-8');
        $xml->startElement('lvb_system'); 
        //Line       
        foreach ($line as $dline) {
            //Vehicle Counter
            $count = 0;
            foreach ($vehicle as $count_vehicles) {
                if ($dline['id'] == $count_vehicles['line_id']) {
                    $count++;
                }
            }
            // End Vehicle Counter
            $xml->startElement('line');//Start Line
            $xml->writeAttribute('id', $dline['id']);
            $xml->writeElement('code', $dline['code']); //Code
            $xml->writeElement('type', $dline['type']); //Type
            $xml->writeElement('start_time_operation', $dline['start_time_operation']); //Start-Time
            $xml->writeElement('end_time_operation', $dline['end_time_operation']); //End-Time
            $xml->writeElement('count_vehicles', $count); //Count Vehicle
            $xml->writeElement('map', $dline['map']); //Map
            //Start-End Station ID
            foreach ($station as $dstation) {
                if ($dstation['position_station'] == 'START' && $dstation['line_id'] == $dline['id']) {
                    $xml->startElement('start_station');
                    $xml->writeAttribute('id', $dstation['id']);
                    $xml->writeElement('name', $dstation['name']);
                    $xml->endElement();
                }
                if ($dstation['position_station'] == 'END' && $dstation['line_id'] == $dline['id']) {
                    $xml->startElement('end_station');
                    $xml->writeAttribute('id', $dstation['id']);
                    $xml->writeElement('name', $dstation['name']);
                    $xml->endElement();
                }
            }
            //Intermediate Station            
            foreach ($station as $dstation) {
                if (($dstation['position_station'] == '' || $dstation['position_station'] == 'INTER') && $dstation['line_id'] == $dline['id']) { 
                    $xml->startElement('intermediate_stations');
                    $xml->writeAttribute('id', $dstation['id']);
                    $xml->writeElement('name', $dstation['name']);
                    $xml->endElement(); //End Intermediate Station
                }                
            }  
            //Vehicles Line           
            foreach ($vehicle as $dvehicle) {
                if ($dvehicle['line_id'] == $dline['id']) { 
                    $xml->startElement('vehicles_line');
                    $xml->writeAttribute('id', $dvehicle['id']);
                    $xml->writeElement('name', $dvehicle['name']);
                    $xml->writeElement('capacity', $dvehicle['capacity']);
                    foreach ($driver as $ddriver) {
                        if ($ddriver['vehicle_id'] == $dvehicle['id']) {
                            $xml->startElement('driver');
                            $xml->writeAttribute('id', $ddriver['id']);
                            $xml->writeElement('name', $ddriver['name']);
                            $xml->writeElement('birth_date', $ddriver['birth_date']);
                            $xml->writeElement('email', $ddriver['email']);
                            $xml->writeElement('phone', $ddriver['phone']);
                            $xml->writeElement('avatar', $ddriver['avatar']);
                            $xml->endElement();//End Driver
                        }
                    }
                    $xml->endElement(); //End Intermediate Station
                }                
            }           
            $xml->endElement(); //End Line
        }
        $xml->endElement(); //End Root
        $xml->endDocument();//End document
        header('Content-Type: text/xml');
        echo $xml->outputMemory();
    }
}